package leetcode.backtrace;

import java.util.ArrayList;
import java.util.List;

public class Lc131 {

    List<List<String>> ans = new ArrayList<>();

    public List<List<String>> partition(String s) {
        List<String> list = new ArrayList<>();
        backTack(s, 0, list);
        return ans;
    }

    private void backTack(String s, int start, List<String> list) {

        if (start == s.length()) {
            ans.add(new ArrayList<>(list));
            return;
        }

        for (int i = start + 1; i <= s.length(); i++) {
            String temp = s.substring(start, i);
            if (isPalindrome(temp)) {
                list.add(temp);
                backTack(s, i, list);
                list.remove(list.size() - 1);
            }
        }
    }

    private boolean isPalindrome(String temp) {
        int i = 0, j = temp.length() - 1;
        while (i < j) {
            if (temp.charAt(i) != temp.charAt(j))
                return false;
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "aab";
        Lc131 lc131 = new Lc131();
        System.out.println(lc131.partition(s));
    }


}
